package networkLoader;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Optional;

import network.Device;
import network.Network;
import network.Port;
import network.Rule;

public class Dumper {
    public static Network n = FattreeNetwork.getNetwork(48);
    // public static Network n = FBNetwork.getNetwork();

    public static void dumpTopo() throws IOException {
        HashSet<String> cache = new HashSet<>();
        BufferedWriter writer = new BufferedWriter(new FileWriter("dataset/fbtopo.txt"));
        for (Device device : n.getAllDevices()) {
            for (Port p : device.getPorts()) {
                if (p.getPeer() != null && !cache.contains(p.getPeer().getName() + p.getName())) {
                    cache.add(p.getName() + p.getPeer().getName());
                    String out = device.getName() + " " + p.getName() + " " + p.getPeer().getDevice().getName() + " " + p.getPeer().getName();
                    writer.write(out + "\n");
                }
            }
        }
        writer.close();
    }
    public static void main(String[] args) throws IOException {
        
        // dumpTopo();
        // System.exit(0);
        
        System.out.println(n.getAllDevices().size());
        System.out.println(n.getInitialRules().size());
        System.exit(0);

        
        for (Device device : n.getAllDevices()) {
            BufferedWriter writer = new BufferedWriter(new FileWriter("dataset/fb/" + device.getName()));
            String dp = "";
            if (device.getName().contains("rsw")) {
                dp = device.getPorts().stream().map(Port::getName).filter(p -> p.contains("fsw")).sorted().findFirst().orElse(null);
                String out = "fw 0 0 " + dp;
                writer.write(out + "\n");
            } else if (device.getName().contains("fsw")) {
                dp = device.getPorts().stream().map(Port::getName).filter(p -> p.contains("ssw")).sorted().findFirst().orElse(null);
                String out = "fw 0 0 " + dp;
                writer.write(out + "\n");
            }
            for (Rule rule : device.getInitialRules()) {
                String out = ("fw " + rule.getMatch().longValue() + " " + rule.getPrefix() + " " + rule.getOutPort().getName());
                writer.write(out + "\n");
            }
            writer.close();
        }
        // for (int i = 0; i < saves.size(); i++) {
        //     System.out.println(saves.get(i));
        //     System.out.println(loads.get(i));
        //     writer.write(saves.get(i).toString());
        //     writer.write("\n");
        //     writer.write(loads.get(i).toString());
        //     writer.write("\n");
        // }
        // writer.close();
    }
    
}
